package LinkedList;

import java.util.ArrayList;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {
        LinkedList linkedList=new LinkedList();
        test1(linkedList);
        test2(linkedList);
        test3(linkedList);
        test4(linkedList);
        test5();
    }

    public static void test1(LinkedList linkedList) {
        System.out.println(("=============test1=============="));
        linkedList.addLast(1);
        linkedList.addLast(2);
        linkedList.addLast(3);
        linkedList.addLast(4);
        linkedList.addFirst(5);
        linkedList.display();
        System.out.println(linkedList.size());
        System.out.println(linkedList.contains(4));
        linkedList.addIndex(5,6);
        linkedList.display();
        linkedList.get(3);
        linkedList.display(linkedList.get(3));
        System.out.println("=============test2==============");
    }

    public static void test2(LinkedList linkedList) {
        //System.out.println(linkedList.remove(1));
        System.out.println(linkedList.removePos(4));
        linkedList.display();
        linkedList.clear();
        System.out.println("=============test3==============");
    }

    public static void test3(LinkedList linkedList) {
        linkedList.addLast(2);
        linkedList.addLast(2);
        linkedList.addLast(3);
        linkedList.addLast(2);
        linkedList.addFirst(5);
        linkedList.addFirst(2);
        linkedList.addFirst(2);
        linkedList.display();
        linkedList.removeAll(2);
        linkedList.display();
        linkedList.clear();
        System.out.println("=============test4==============");
    }

    public static void test4(LinkedList linkedList) {
        linkedList.addIndex(0,10);
        linkedList.remove(10);
        linkedList.display();
        System.out.println("=============test5==============");
    }

    public static void test5() {
        ArrayList<String> list=new ArrayList<>();
        //在构造中的形参类型是:Collection<? extends E> c  E是LinkedList传入的参数类型，所以给构造方法传参必须是实现了Collection接口而且传入的数据类型必须是E的子类或本身
        java.util.LinkedList<String> linkedList=new java.util.LinkedList<>(list);//构造方法传入的参数中的参数类型必须要是LinkedList传入参数类型的子类或者本身
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        linkedList.addAll(list);

        System.out.println(linkedList);                                //直接遍历(重写的toString)

        for (int i = 0; i < linkedList.size(); i++) {
            System.out.print(linkedList.get(i)+" ");
        }
        System.out.println();

        for (String x:linkedList) {
            System.out.print(x+" ");
        }
        System.out.println();

        ListIterator<String> it=linkedList.listIterator();                  //迭代器遍历
        while(it.hasNext()) {
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("-反向遍历-");
        ListIterator<String> it1=linkedList.listIterator(linkedList.size());                  //迭代器遍历
        while(it1.hasPrevious()) {
            System.out.print(it1.previous()+" ");
        }
    }
}
